package com.benjaminwan.read.free.views

import android.content.Context
import android.text.Html
import android.text.util.Linkify
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatButton
import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView
import com.airbnb.epoxy.TextProp
import com.benjaminwan.read.free.R
import com.benjaminwan.read.free.model.bean.ContentAttach
import com.benjaminwan.read.free.model.bean.ContentCode
import com.benjaminwan.read.free.model.bean.ContentGoToFloor
import com.benjaminwan.read.free.model.bean.ContentQuote
import com.benjaminwan.read.free.model.bean.ContentText
import com.benjaminwan.read.free.model.bean.Contents
import com.benjaminwan.read.free.utils.HiUtils.removeLeadingBr
import com.benjaminwan.read.free.utils.toClipboard
import com.benjaminwan.read.free.widget.TextViewWithLinkClickable

@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT)
class ThreadDetailItemView @JvmOverloads constructor(
    context: Context,
    private val attrs: AttributeSet? = null,
    private val defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private val authorTV by lazy { findViewById<TextView>(R.id.authorTV) }
    private val timeTV by lazy { findViewById<TextView>(R.id.timeTV) }
    private val floorTV by lazy { findViewById<TextView>(R.id.floorTV) }
    private val postStatusTV by lazy { findViewById<TextView>(R.id.postStatusTV) }
    private val contentItemLayout by lazy { findViewById<LinearLayout>(R.id.contentItemLayout) }
    private val quoteAuthorTV by lazy { findViewById<TextView>(R.id.quoteAuthorTV) }
    private val quotePostTimeTV by lazy { findViewById<TextView>(R.id.quotePostTimeTV) }
    private val quoteNoteTV by lazy { findViewById<TextView>(R.id.quoteNoteTV) }
    private val quoteContentTV by lazy { findViewById<TextViewWithLinkClickable>(R.id.quoteContentTV) }
    private val codeTv by lazy { findViewById<TextView>(R.id.codeTv) }
    private val copyBtn by lazy { findViewById<AppCompatButton>(R.id.copyBtn) }

    init {
        View.inflate(context, R.layout.rv_thread_detail_item, this)
    }

    @TextProp
    fun setAuthor(content: CharSequence) {
        authorTV.text = content
    }

    @TextProp
    fun setTime(content: CharSequence) {
        timeTV.text = content
    }

    @TextProp
    fun setFloor(content: CharSequence) {
        floorTV.text = content
    }

    @TextProp
    fun setPostStatus(content: CharSequence?) {
        if (content.isNullOrEmpty()) {
            postStatusTV.visibility = View.GONE
        } else {
            postStatusTV.visibility = View.VISIBLE
            postStatusTV.text = content
        }
    }

    @JvmOverloads
    @ModelProp
    fun setContents(contents: Contents? = null) {
        if (contents == null) return
        contentItemLayout.removeAllViews()
        contentItemLayout.bringToFront()
        contents.list.forEach { content ->
            when (content) {
                is ContentText -> {
                    val tv = TextViewWithLinkClickable(context, attrs, defStyleAttr)
                    tv.setPadding(8, 8, 8, 8)
                    tv.text = Html.fromHtml(
                        removeLeadingBr(content.content), Html.FROM_HTML_MODE_COMPACT
                    )
                    tv.autoLinkMask = Linkify.WEB_URLS
                    contentItemLayout.addView(tv)
                }

                is ContentAttach -> {
                    val tv = TextViewWithLinkClickable(context, attrs, defStyleAttr)
                    tv.setPadding(8, 8, 8, 8)
                    tv.text =
                        Html.fromHtml(removeLeadingBr(content.content), Html.FROM_HTML_MODE_COMPACT)
                    tv.autoLinkMask = Linkify.WEB_URLS
                    contentItemLayout.addView(tv)
                }
                /*is ContentAttachInfo -> {
                    val tv = TextViewWithLinkClickable(context, attrs, defStyleAttr)
                    tv.setPadding(8, 8, 8, 8)
                    tv.text = content.content
                    contentItemLayout.addView(tv)
                }*/
                is ContentQuote -> {
                    LayoutInflater.from(this.context)
                        .inflate(R.layout.item_quote_text, this.contentItemLayout)
                    quoteAuthorTV.text = content.author
                    quotePostTimeTV.text = content.time
                    quoteNoteTV.text = "${content.to}"
                    quoteContentTV.text =
                        Html.fromHtml(
                            removeLeadingBr(content.postText),
                            Html.FROM_HTML_MODE_COMPACT
                        )
                    quoteContentTV.autoLinkMask = Linkify.WEB_URLS
                }

                is ContentGoToFloor -> {
                    LayoutInflater.from(this.context)
                        .inflate(R.layout.item_quote_text, this.contentItemLayout)
                    quoteAuthorTV.text = content.author
                    quoteNoteTV.text = "${content.floor}#"
                    quoteContentTV.text =
                        Html.fromHtml(removeLeadingBr(content.content), Html.FROM_HTML_MODE_COMPACT)
                    quoteContentTV.autoLinkMask = Linkify.WEB_URLS
                }

                is ContentCode -> {
                    LayoutInflater.from(this.context)
                        .inflate(R.layout.item_code_text, this.contentItemLayout)
                    codeTv.text = content.text
                    copyBtn.setOnClickListener {
                        context.toClipboard(content.text)
                    }
                }

                else -> {

                }
            }
        }
    }

    /*@CallbackProp
    fun onClickListener(listener: View.OnClickListener?) {
        threadDetailLayout.setOnClickListener(listener)
    }*/
}